package tulin123

import (
	"fmt"
	"gitee.com/ifinder/chat-robot/domain"
	"gitee.com/ifinder/chat-robot/utils"
	"github.com/bitly/go-simplejson"
)
type Tunlin123 struct {
	Token string
}

func NewTunlin123(token string) *Tunlin123 {
	if token==""{
		token="e97c98fb4147b79746a3664b3257d9b0"
	}
	robot:=Tunlin123{Token:token}
	return &robot
}

func (this *Tunlin123) Say(sayReq *domain.SayReq) (*domain.SayResp,error){

	var resp domain.SayResp
	apiUrl := fmt.Sprintf("http://www.tuling123.com/openapi/api?key=%s&info=%s",this.Token,sayReq.Content)
	ret,err:=utils.HTTPGet(apiUrl)

	if err!=nil{
		return nil,err
	}

	myJson, err := simplejson.NewJson(ret)
	if err!=nil{
		return nil,err
	}

	//fmt.Println(string(res))

	code,_:=myJson.Get("code").Int()
	if code ==10001 || code ==40002 || code==100000{
		txt,err:=myJson.Get("text").String()
		if err!=nil{
			return nil,err
		}
		resp.Content = txt
	}
	return &resp,nil
}
